local start, duration = GetSpellCooldown("Corruption");
local gcd = start + duration;
local function GetCooldown(id)
  local start, duration = GetSpellCooldown(id);
  local cd = start + duration - gcd;
  if cd < 0 then return 0 end
  return cd
end



local AgominDuration = 4
local UAminDuration = 3;  
local CorrminDuration = 3;  
local HauntminDuration = 4;

if (not (UnitExists("target") and UnitCanAttack("player", "target"))) or (UnitIsDeadOrGhost("target")) then
  return
end


local _, _, _, duration = IconAura("HARMFUL", "target", "Agony", "player");
if (not duration) then
  return IconSpell("Agony", true);
end

local _, _, start, Agoduration = IconAura("HARMFUL", "target", "Agony", "player");
if (start + Agoduration - GetTime() < AgominDuration) then
    return IconSpell("Agony", true);
end

local _, _, _, duration = IconAura("HARMFUL", "target", "Corruption", "player");
if (not duration) then
  return IconSpell("Corruption", true);
end

local _, _, _, duration = IconAura("HARMFUL", "target", "Unstable Affliction", "player");
if (not duration) then
  return IconSpell("Unstable Affliction", true);
end


local _, _, start, UAduration = IconAura("HARMFUL", "target", "Unstable Affliction", "player");
if (start + UAduration - GetTime() < UAminDuration) then
    return IconSpell("Unstable Affliction", true);
end

local _, _, start, Corrduration = IconAura("HARMFUL", "target", "Corruption", "player");
if (start + Corrduration - GetTime() < CorrminDuration) then
    return IconSpell("Corruption", true);
end

if (UnitPower("Player", 7) > 1) then
	local _, _, start, Hauntduration = IconAura("HARMFUL", "target", "Haunt", "player");
	if (not Hauntduration) then
		return IconSpell("Haunt", true);
	end
	if (Hauntduration) then
		if (start + Hauntduration - GetTime() < HauntminDuration) then
			return IconSpell("Haunt", true);
		end
	end
end

local c = floor(UnitHealth("target") / UnitHealthMax("target") * 100)
if (c <= 20) then
    return IconSpell("Drain Soul", true)
end

local _, _, start, duration = IconAura("HARMFUL", "target", "Curse of the Elements");
if (not duration) then
        return IconSpell ("Curse of the Elements", true);
end

return IconSpell("Malefic Grasp", true);
